package com.boxedinsoftware.betamax.dial;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class RulesManager extends Activity
{
	
	
	
	AccountsSQLHelper dh;
	ArrayList<RulesResults> searchResults;
	ListView lv1;
	
		@Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.rules_view);
	        this.dh = new AccountsSQLHelper(RulesManager.this.getApplicationContext());
	        searchResults = refresh();
	        
	        lv1 = (ListView) findViewById(R.id.rules_list);
	        lv1.setAdapter(new RulesBaseAdapter(this, searchResults));
	        
	        lv1.setOnItemClickListener(new OnItemClickListener() {
	         public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
	          Object o = lv1.getItemAtPosition(position);
	          RulesResults fullObject = (RulesResults)o;
	          Toast.makeText(RulesManager.this, "To delete rules #" + fullObject.getID() +" for " + fullObject.getObject() + " please long click on the row.", Toast.LENGTH_SHORT).show();
	         }  
	        });
	        
	        lv1.setOnItemLongClickListener(new OnItemLongClickListener() {
		         public boolean onItemLongClick(AdapterView<?> a, View v, int position, long id) { 
		          Object o = lv1.getItemAtPosition(position);
		          final RulesResults fullObject = (RulesResults)o;
		          AlertDialog.Builder builder = new AlertDialog.Builder(RulesManager.this);
		          builder.setMessage("Are you sure you want to delete rule #" + fullObject.getID() + " from Betamax Dialer Pro ?")
		                 .setCancelable(false)
		                 .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
		                     public void onClick(DialogInterface dialog, int id) {
		                          RulesManager.this.dh.deleteRowRules(fullObject.getID());
		                          searchResults = refresh();
		      					  lv1.setAdapter(new RulesBaseAdapter(RulesManager.this, searchResults));
		                     }
		                 })
		                 .setNegativeButton("No", new DialogInterface.OnClickListener() {
		                     public void onClick(DialogInterface dialog, int id) {
		                          dialog.cancel();
		                     }
		                 });
		          AlertDialog alert = builder.create();
		          alert.show();
		          return true;
		         }  
		        });
	        
	       Button deleteAll = (Button)findViewById(R.id.delete_rules_button);
	       
	       deleteAll.setOnClickListener(new OnClickListener(){

			public void onClick(View arg0) {
				AlertDialog.Builder builder = new AlertDialog.Builder(RulesManager.this);
		          builder.setMessage("Are you sure you want to delete all rules ?")
		                 .setCancelable(false)
		                 .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
		                     public void onClick(DialogInterface dialog, int id) {
		                    	 RulesManager.this.dh.deleteAllRules();
		                          searchResults = refresh();
		      					  lv1.setAdapter(new RulesBaseAdapter(RulesManager.this, searchResults));
		                     }
		                 })
		                 .setNegativeButton("No", new DialogInterface.OnClickListener() {
		                     public void onClick(DialogInterface dialog, int id) {
		                          dialog.cancel();
		                     }
		                 });
		          AlertDialog alert = builder.create();
		          alert.show();
				
				
			}
	    	   
	    	   
	       });
	       
		}
		
		
		private ArrayList<RulesResults> refresh(){
		     ArrayList<RulesResults> results = new ArrayList<RulesResults>();
		     
		     RulesResults sr1;
		     Cursor cursor = this.dh.selectAllRules();
		        while (cursor.moveToNext()) {
		        	sr1 = new RulesResults();
		            sr1.setID(cursor.getInt(0));
		            sr1.setObject(cursor.getString(1));
		            sr1.setChoice(cursor.getInt(2));
		            results.add(sr1);
		          }
		         
		     return results;
		    }
}